

#include "log.hpp"
// void test_log()
// {
//   log::Logger::ptr logger = log::LoggerManager::getInstance().getLogger("async_logger");
//         logger->debug(__FILE__, __LINE__, "%s", "测试日志");
//         logger->info(__FILE__, __LINE__, "%s", "测试日志");
//         logger->warn(__FILE__, __LINE__, "%s", "测试日志");
//         logger->error(__FILE__, __LINE__, "%s", "测试日志");
//         logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
//         size_t  count = 0;
//         while (count < 500000)
//         {
//           logger->fatal(__FILE__,__LINE__, "测试日志-%d", count++);
         
//         }

// }

void test_log()
{
  log::Logger::ptr logger = log::LoggerManager::getInstance().getLogger("async_logger");
        DEBUG( "%s", "测试日志");
        INFO( "%s", "测试日志");
        WARN( "%s", "测试日志");
        ERROR( "%s", "测试日志");
        FATAL("%s", "测试日志");
        size_t  count = 0;
        while (count < 500000)
        {
          FATAL( "测试日志-%d", count++);
         
        }

}


int main()
{
      //测试sink接口
        // log::LogMsg msg( 63, log::LogLevel::value::INFO, "main.c" ,"root", "格式化");
        // log:: Formatter fmt;
        // std::string str = fmt.format(msg);
        // log::LogSink::ptr stdout_lsp = log::SinkFactory::create<log::StdoutSink>();
        // log::LogSink::ptr fileout_lsp = log::SinkFactory::create<log::FileSink>("./logfile/test.log");
        // log::LogSink::ptr rollout_lsp = log::SinkFactory::create<log::RollBySizeSink>("./logfile/roll-",1024*1024);
        // stdout_lsp->log(str.c_str(),str.size());
        // fileout_lsp->log(str.c_str(),str.size());
        // size_t cursize = 0;
        // size_t count = 0;
        // while(cursize < 1024*1024*10)
        // {
        //         std::string tmp = str+std::to_string(count++);
        //         rollout_lsp->log(tmp.c_str(), tmp.size());
        //         cursize += tmp.size();

        // }
       //测试sink接口



        // std::cout<<log::util::Date::now()<< std::endl;
        // std::string pathname = "./sfsf/avv/abc.txt";
        // log::util::File:: CreateDirectory(log::util::File:: Path(pathname));


        //测试同步日志器模块
        // std::string logger_name = "sync_logger";
        // log::LogLevel::value limit = log::LogLevel::value::WARN;
        // log::Formatter::ptr fmt(new log::Formatter("[%d{%H:%M:%S}][%c][%f:%l][%p]%T%m%n"));//创建日志格式化器
        // log::LogSink::ptr stdout_lsp = log::SinkFactory::create<log::StdoutSink>();
        // log::LogSink::ptr fileout_lsp = log::SinkFactory::create<log::FileSink>("./logfile/test.log");
        // log::LogSink::ptr rollout_lsp = log::SinkFactory::create<log::RollBySizeSink>("./logfile/roll-",1024*1024);
        // std::vector<log::LogSink::ptr> sinks = {stdout_lsp, fileout_lsp, rollout_lsp}; //落地模式指针数组
        // log::Logger::ptr logger(new log::SyncLogger(logger_name, limit, fmt, sinks));
        // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
        // logger->info(__FILE__, __LINE__, "%s", "测试日志");
        // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
        // logger->error(__FILE__, __LINE__, "%s", "测试日志");
        // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
        // size_t cursize = 0, count = 0;
        // while (cursize < 1024 * 1024 * 10)
        // {
        //   logger->fatal(__FILE__,__LINE__, "测试日志-%d", count++);
        //   cursize += 20;
        // }
        //测试同步日志器模块
        



        //局部日志器构造测试
        // std::unique_ptr<log::LoggerBuilder> builder(new log::LocalLoggerBuilder());
        // builder->buildLoggerName("sync_logger");
        // builder->buildLoggerLevel(log::LogLevel::value::WARN);
        // builder->buildFommater("%m%n");
        // builder->buildLoggerType(log::LoggerType::LOGGER_SYNC);
        // builder->buildSink<log::FileSink>("./logfile/test.log");
        // //builder->buildSink<log::StdoutSink>();
        // log::Logger::ptr logger =builder->build();
        // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
        // logger->info(__FILE__, __LINE__, "%s", "测试日志");
        // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
        // logger->error(__FILE__, __LINE__, "%s", "测试日志");
        // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
        // size_t cursize = 0, count = 0;
        // while (cursize < 1024 * 1024 * 10)
        // {
        //   logger->fatal(__FILE__,__LINE__, "测试日志-%d", count++);
        //   cursize += 20;
        // }
        //局部日志器构造测试
        

        //测试缓冲区buffer
        //读取文件数据，一点一点写入缓冲区，然后将缓冲区的数据写入到文件 ，判断生成的新文件和源文件是否一致
        // std::ifstream ifs("./logfile/test.log", std::ios::binary);
        // if(ifs.is_open() == false) {std::cout<<"open file is failed"<<std::endl; return -1;}
        // ifs.seekg(0, std::ios::end);//读写位置跳转到文件末尾
        // size_t fsize = ifs.tellg();//获取当前读写位置相对于起始位置的偏移量
        // ifs.seekg(0, std::ios::beg);//读写位置跳转到文件开始
        // std::string body;
        // body.resize(fsize);
        // ifs.read(&body[0], fsize);
        // if(ifs.good()==false){std::cout<<"read file is error"<<std::endl; return -1;}
        // ifs.close();

        // log::Buffer buffer;

        // for(int i = 0; i < body.size(); i++)
        // {
        //   buffer.push(&body[i], 1);
        // }
        // std::ofstream ofs("./logfile/temp.txt", std::ios::binary);
        // size_t size = buffer.readAbleSize();
        // for(int i = 0; i < size; i++)
        // {
        //   ofs.write(buffer.begin(),1);
        //   buffer.moveReader(1);
        // }

        // ofs.close();

        //测试缓冲区buffer



        // //局部异步日志器构造测试
        // std::unique_ptr<log::LoggerBuilder> builder(new log::LocalLoggerBuilder());
        // builder->buildLoggerName("Async_logger");
        // builder->buildLoggerLevel(log::LogLevel::value::WARN);
        // builder->buildFommater("[%c]%m%n");
        // builder->buildLoggerType(log::LoggerType::LOGGER_ASYNC);
        // builder->buildEnableUnSafeAsync();
        // builder->buildSink<log::FileSink>("./logfile/test.log");
        // builder->buildSink<log::StdoutSink>();
        // log::Logger::ptr logger =builder->build();
        // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
        // logger->info(__FILE__, __LINE__, "%s", "测试日志");
        // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
        // logger->error(__FILE__, __LINE__, "%s", "测试日志");
        // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
        // size_t  count = 0;
        // while (count < 500000)
        // {
        //   logger->fatal(__FILE__,__LINE__, "测试日志-%d", count++);
         
        // }


        std::unique_ptr<log::LoggerBuilder> builder(new log::GlobalLoggerBuilder());
        builder->buildLoggerName("async_logger");
        builder->buildLoggerLevel(log::LogLevel::value::WARN);
        builder->buildFommater("[%c]%m%n");
        builder->buildLoggerType(log::LoggerType::LOGGER_ASYNC);
        builder->buildEnableUnSafeAsync();
        builder->buildSink<log::FileSink>("./logfile/test.log");
        builder->buildSink<log::StdoutSink>();
        builder->build();
     
       test_log();

        return 0;
}